package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Goods;
import com.luo.model.Result;
import com.luo.model.ResultObj;
import com.luo.service.ClassService;
import com.luo.service.GoodService;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/1/12 18:11
 */
@WebServlet("/updateGood")
@MultipartConfig
public class GoodUpdateController extends HttpServlet {
    GoodService goodService = new GoodService();
    ClassService classService = new ClassService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ResultObj result = new ResultObj();
        result.setCode("-1");
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String inventory = req.getParameter("inventory");
        String discount = req.getParameter("discount");
        String hotNum = req.getParameter("hotNum");
        String picture = req.getParameter("picture");
        String oneClass = req.getParameter("oneClassName");
        String twoClass = req.getParameter("twoClassName");
        //根据类别名称查询类别id
        String oneClassId = classService.queryOneClassByName(oneClass);
        String twoClassId = classService.queryTwoClassByName(twoClass);

        String describe = req.getParameter("describe");
        String id = req.getParameter("id");
        if (id == null){
            result.setMsg("修改失败【id不能为空】");
            String jsonString = JSON.toJSONString(result);
            resp.getWriter().write("jsonString");
            resp.getWriter().close();
            return;
        }
        Goods goods = new Goods();
        goods.setId(Integer.parseInt(id));
        goods.setName(name);
        goods.setDesc(describe);
        goods.setDiscount(discount);
        goods.setHotNum(hotNum);
        goods.setInventory(inventory);
        goods.setPicture(picture);
        goods.setPrice(price);
        goods.setOneClassId(oneClassId);
        goods.setTwoClassId(twoClassId);
        int i = goodService.updateGood(goods);
        result.setCode("1");
        result.setMsg("修改成功");
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
